$(function () {
    var url = location.search;
    var brandName=decodeURI(url);
    var brandId="";
    if(url!=null && url!="") {
        brandName = brandName.split("=")[1];
        vm.q.brandId = brandName;
        vm.q.source=1;
        console.log(vm.q.brandId);
        vm.reload();
    }
})
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bmaterialinfo/queryList',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id',align: "center", width: 60, key: true},
            { label: '素材配文', name: 'articleDesc', index: 'article_desc',align: "center", width: 220},
            { label: '创建时间', name: 'createTime',dataType: "date", sortable: true, index: 'create_time',align: "center", width: 140 },
            { label: '品牌/分类', name: 'articleTypeName', index: 'articleTypeName',align: "center", width: 80 },
            { label: '创建者', name: 'userName', index: 'userName',align: "center", width: 50,
                formatter:function(value,options,rowData){
                    var source=rowData["articleSource"]
                    if(source==1){
                        return value;
                    }else{
                        var userId = rowData["userId"];
                        if(value==""){
                            return "";
                        }else {
                            return "<a onclick=vm.openUser("+userId+")>" + value + "</a>";
                        }
                    }
                }
            },
            { label: '分享', name: 'articleShareCount',dataType: "int", sortable: true, index: 'article_share_count',align: "center", width: 40 },
            { label: '热度', name: 'articleHotScore',dataType: "int", sortable: true, index: 'article_hot_score',align: "center", width: 50
            },
            { label: '查看', name: 'articleOutReadCount',dataType: "int", sortable: true, index: 'article_out_read_count',align: "center", width: 40 },
            { label: '热门', name: 'articleRecommendStatus',sortable: false, index: 'articleRecommendStatus',align: "center", width: 40 ,
                formatter:function(value,options,rowData){
                    var content="";
                    var id=rowData["id"];
                    if( value===0 || value=="" || value==null){
                        content="<a style='text-align: center'onclick=vm.updateStatus('"+id+"',3)>"+"否"+"</a>"
                    }else if(value==1){
                        content= "<strong style='color: red'>"+'是'+"</>"
                    }
                    return content;
                }},
            { label: '来源', name: 'articleSource', index: 'article_source',align: "center", width: 40 ,
                formatter:function(value,options,rowData){
                    var thirdSource=rowData["thirdArticleSource"];
                    var source=rowData["articleSource"];
                    var content="";
                    if(source==1&&thirdSource==1){
                        content= "好文";
                    }else if(source==1&&thirdSource==2){
                        content= "V名片"
                    }else if(source==1&&thirdSource!=null&&thirdSource.length>1){
                        content= thirdSource;
                    }else if( value===1 ){
                        content= "平台";
                    }else if(value==2){
                        content= "用户";
                    }else if(value==3){
                        content= '用户'
                    }
                    return content;
                }
            },
            { label: '状态', name: 'articleStatus', index: 'article_status',align: "center", width: 40 ,
                formatter:function(value,options,rowData){
                    var content="";
                    if( value===1 ){
                        content= '正常';
                    }else if(value==0){
                        content= "<p style='color: red'>"+'隐藏'+"</>"
                    }
                    return content;
                }
            },
            { label: '操作', name: '', index: '',align: "center", width: 130,
                formatter:function(value,options,rowData){
                    var status=rowData["articleStatus"];
                    var id=rowData["id"];
                    var content="";
                    if( status===1 ){
                        content= "<a style='text-align: center' class='btn btn-primary' onclick=vm.updateStatus('"+id+"',0)>"+"隐藏"+"</a>";
                    }else if(status==0||status==2){
                        content= "<a style='text-align: center' class='btn btn-primary' onclick=vm.updateStatus('"+id+"',1)>"+"恢复"+"</a>";
                    }
                    content=content+ "<a style='text-align: center' class='btn btn-primary' onclick=vm.editInfo('"+id+"',1)>"+"修改"+"</a>";
                    return content;
                }
            }
        ],
        viewrecords: true,
        height: "100%",
        rowNum: 12,
        rowList : [12,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        postData:{'articleTitle':vm.q.articleTitle,'articleTypeName': vm.q.articleTypeName,"brandId":vm.q.brandId,"source":vm.q.source},
        gridComplete:function(){
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});
setTimeout(function (){
    $.ajax({
        type: "POST",
        async:false,
        url: baseURL + 'sys/bbrandinfo/queryAll',
        contentType: "application/json",
        success: function(r){
            if(r.code==0){
                //vm.bbrandinfo=r.brandInfo;
                for(var i=0;i<r.brandInfo.length;i++){
                    $("#brandName").append(
                        "<option value=" + r.brandInfo[i].brandName +">"+"</option>");
                }
            }
        }
    });
}, 2000);
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            articleTypeName:"",
            articleTitle:"",
            brandId:"",
            desc:"",
            articleSource:"",
            source:""
        },
        showList: true,
        title: null,
        bVideoInfo: {},
        bbrandinfo:{},
        articleTypeName:null,
        articleTitle:null,
        barticletypeinfo:{},
        pp:'pp',
        lx:'lx',
        page:0,
        bMaterialInfoArticleImgUrl:[]
    },
    methods: {
        query: function () {
            vm.q.brandId="";
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.bVideoInfo = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        status:function(event){
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.bVideoInfo={};
            vm.bVideoInfo.articleStatus=event;
            vm.bVideoInfo.id=id;
            var url = "sys/bvideoinfo/update";
            confirm('确定隐藏吗状态？', function() {
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.bVideoInfo),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                                //window.location.href="../../../templates/modules/sys/buserinfo.html";
                                // window.location.href=baseURL+"sys/permissions/index/"+userId;
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });

        },
        updateStatus: function (ids,event) {
            vm.bVideoInfo={};
            confirm('确定修改状态？', function(){
                if(event==3 || event=="3"){
                    vm.bVideoInfo.articleRecommendStatus=1;
                }else{
                    vm.bVideoInfo.articleStatus=event;
                }
                vm.bVideoInfo.id=ids;
                console.log(vm.bVideoInfo)
                var url = "sys/bvideoinfo/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.bVideoInfo),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                                //window.location.href="../../../templates/modules/sys/buserinfo.html";
                                // window.location.href=baseURL+"sys/permissions/index/"+userId;
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        editInfo: function (ids,event) {
            window.open("../sys/bmaterial_edit.html?id="+ids);
        },
        addInfo: function (ids,event) {
            window.open("../sys/bmaterial_add.html?id="+ids);
        },
        updateInfo: function (event) {
            var url = vm.bVideoInfo.id == null ? "sys/bmaterialinfo/save" : "sys/bmaterialinfo/updateInfo";

            vm.bVideoInfo.brandName=$('#brandName').val();
            vm.bVideoInfo.articleTypeName=$('#articleTypeName').val();
            vm.bVideoInfo.articleImgUrl = vm.bMaterialInfoArticleImgUrl.join(',');
            if(vm.bVideoInfo.brandName=="" && vm.bVideoInfo.articleTypeName==""){
                alert("请选择品牌或者分类,注意只能选择其中一项");
                return;
            }

            if(vm.bVideoInfo.brandName!="" && vm.bVideoInfo.articleTypeName!=""){
                alert("品牌或者分类,只能选择其中一项");
                return;
            }
             vm.bVideoInfo.articleDesc = $('.summernote').summernote("code");
            console.log(vm.bVideoInfo);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=UTF-8",
                data:JSON.stringify(vm.bVideoInfo),
                dataType: "JSON",
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            window.close();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        saveInfo: function (event) {
            var url = vm.bVideoInfo.id == null ? "sys/bmaterialinfo/save" : "sys/bmaterialinfo/save";

            var strArticleTypeNames="";
            if($(".pArticleTypeName").length>0){
                $(".pArticleTypeName").each(function(){

                    strArticleTypeNames=strArticleTypeNames+","+$(this).attr("value");
                });
                strArticleTypeNames=strArticleTypeNames.substring(1);
            }
            vm.bVideoInfo.brandName=strArticleTypeNames;

            vm.bVideoInfo.articleTypeName=$('#articleTypeName').val();

            vm.bVideoInfo.articleImgUrl = vm.bMaterialInfoArticleImgUrl.join(',');
            if(vm.bVideoInfo.brandName=="" && vm.bVideoInfo.articleTypeName==""){
                alert("请选择品牌或者分类,注意只能选择其中一项");
                return;
            }

            if(vm.bVideoInfo.brandName!="" && vm.bVideoInfo.articleTypeName!=""){
                alert("品牌或者分类,只能选择其中一项");
                return;
            }
            vm.bVideoInfo.articleDesc = $('.summernote').summernote("code");
            console.log(vm.bVideoInfo);
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json;charset=UTF-8",
                data:JSON.stringify(vm.bVideoInfo),
                dataType: "JSON",
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            window.close();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/bvideoinfo/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $("#type").empty();
            $.get(baseURL + "sys/bvideoinfo/info/"+id, function(r){
                vm.bVideoInfo = r.bVideoInfo;
                if(r.type==1) {
                    $.get(baseURL + "sys/barticletypeinfo/list/", function (r) {
                        vm.barticletypeinfo = r.page.list;
                        $("#type").append(
                            "<option value=" + vm.lx + ">" + '请选择分类' + "</option>");
                        for (i = 0; i < vm.barticletypeinfo.length; i++) {
                            $("#type").append(
                                "<option value=" + vm.barticletypeinfo[i].id + ">" + vm.barticletypeinfo[i].articleTypeName + "</option>");
                            $("#type").find("option[value='" + vm.bVideoInfo.articleTypeId + "']").attr("selected", true);
                        }
                    });
                }else{
                    $.get(baseURL + "sys/bbrandinfo/list/", function (r) {
                        vm.barticletypeinfo = r.page.list;
                        $("#type").append(
                            "<option value=" + vm.pp + ">" + '请选择品牌' + "</option>");
                        for (i = 0; i < vm.barticletypeinfo.length; i++) {
                            $("#type").append(
                                "<option value=" + vm.barticletypeinfo[i].id + ">" + vm.barticletypeinfo[i].brandName + "</option>");
                            $("#type").find("option[value='" + vm.bVideoInfo.articleBrandId + "']").attr("selected", true);
                        }
                    });
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = 1;
            console.log(page);
            vm.q.articleTypeName=$("#brandNames").val();
            vm.q.desc=$('#desc option:selected').val();
            vm.q.videoRecommendStatus="";
            var videoSource=$("#articleSource").val();
            var source="";
            if(videoSource=="管理员"){
                source=1;
            }else if(videoSource=="用户"){
                source=2;
            }else if(videoSource=="V名片"){
                source=4;
            }else if(videoSource=="公众号"){
                source=5;
            }
            $("#jqGrid").jqGrid('setGridParam',{
                url: baseURL + 'sys/bmaterialinfo/queryList',
                postData:{'desc':vm.q.desc,'articleTitle':vm.q.articleTitle,'articleTypeName': vm.q.articleTypeName,"brandId":vm.q.brandId,"articleRecommendStatus":vm.q.articleRecommendStatus,"source":source},
                page:page
            }).trigger("reloadGrid");
        },
        hot:function () {
            vm.showList = true;
            var page = 1;
            vm.q.articleTypeName=$("#brandNames").val();
            vm.q.videoRecommendStatus=1;
            console.log(vm.q.videoTypeName)
            $("#jqGrid").jqGrid('setGridParam',{
                url: baseURL + 'sys/bvideoinfo/queryList',
                postData:{'articleTitle':vm.q.articleTitle,'articleTypeName': vm.q.articleTypeName,"brandId":vm.q.brandId,"articleRecommendStatus":vm.q.articleRecommendStatus},
                page:page
            }).trigger("reloadGrid");
        },
        openArticle:function (e) {
            layer.open({
                type: 2,
                title: '文章信息',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['420px' , '650px'],
                content: "test.html?id="+e
            });
        },
        openUser:function (e) {
            layer.open({
                type: 2,
                title: '用户信息',
                maxmin: true,
                shadeClose: true, //点击遮罩关闭层
                area : ['600px' , '650px'],
                content: "buserinfo.html?id="+e
            });
        },
        putArticle:function () {
            $.get(baseURL + "sys/bvideoinfo/putArticle", function(r){
                if(r.code==0){
                    alert("入库成功");
                }
            });
        }
    },
});
